package com.zhs.demoutil.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zhs.demoutil.dao.AdminMapper;
import com.zhs.demoutil.entity.Admin;
import com.zhs.demoutil.exception.MyException;
import com.zhs.demoutil.service.AdminService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 一些公用数据（下拉框选择的） 服务实现类
 * </p>
 *
 * @author zhs
 * @since 2020-07-11
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {




    @Autowired
    private AdminMapper adminMapper;


    public void setAdminMapper(AdminMapper adminMapper) {
        this.adminMapper = adminMapper;

        this.baseMapper = adminMapper;
    }

    public AdminMapper getAdminMapper() {
        return adminMapper;
    }

    @Override
    public Admin login(String username, String password) {
        Admin admin = queryByUserName(username);

        if(Objects.isNull(admin)){
            throw new MyException("用户不存在");
        }
        if(Objects.equals(admin.getStatus(), 1)){
            throw new MyException("账号已被冻结，联系管理员恢复");
        }
        String salt = admin.getSalt();
        String pass = SecureUtil.md5(password + salt);
        if(admin.getPassword().equals(pass)){
            Subject subject = SecurityUtils.getSubject();
            subject.login(new UsernamePasswordToken(String.valueOf(admin.getId()),pass));
        }else {
            throw new MyException("密码不正确");
        }
        return admin;
    }

    @Override
    public Admin queryByUserName(String username) {
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.eq("username",username);
        return getOne(adminQueryWrapper);
    }

    @Override
    public Admin queryByMobile(String mobile) {
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.eq("mobile",mobile);
        return getOne(adminQueryWrapper);
    }

    @Override
    public void register(String username, String password, String realName, String mobile, String street, Integer userManager, Integer compare, Integer query) {
        Admin admin = queryByUserName(username);
        if(!Objects.isNull(admin)){
            throw new MyException("用户名已存在");
        }
        if(!Objects.isNull(mobile)){
            Admin byMobile = queryByMobile(mobile);
            if(!Objects.isNull(byMobile)){
                throw new MyException("手机号已存在");
            }
        }
        String salt = RandomUtil.randomString(6);
        admin = new Admin();
        admin.setUsername(username);
        admin.setSalt(salt);
        admin.setRealName(realName);
        admin.setRegion("历城区");
        admin.setStreet(street);
        admin.setUserManager(userManager);
        admin.setCompare(compare);
        admin.setQuery(query);
        String newPassword = SecureUtil.md5(password + salt);
        admin.setPassword(newPassword);
        save(admin);
    }


    @Override
    public void updateUserAndRole(Long adminId, String username, String realName, String mobile, Integer userManager, Integer compare, Integer query) {
        Admin admin = getById(adminId);
        String salt = admin.getSalt();

        Admin admin1 = queryByUserName(username);
        if(!Objects.isNull(admin1)&&!admin1.getId().equals(admin.getId())){
            throw new MyException("用户名已存在");
        }
        if(!Objects.isNull(mobile)){
            Admin admin2 = queryByMobile(mobile);
            if(!Objects.isNull(admin2)&&!admin.getId().equals(admin2.getId())){
                throw new MyException("手机号已存在");
            }
        }
//        String newPassword = SecureUtil.md5(password + salt);
        admin.setUsername(username);
//        admin.setPassword(newPassword);
        admin.setRealName(realName);
        admin.setMobile(mobile);
        admin.setUserManager(userManager);
        admin.setCompare(compare);
        admin.setQuery(query);
        updateById(admin);

    }

    @Override
    public void updatePassword(Long adminId, String oldPassword, String password) {
        Admin byId = getById(adminId);
        if(Objects.isNull(byId)){
            throw new MyException("用户不存在");
        }
        String salt = byId.getSalt();
        String oldPass = SecureUtil.md5(oldPassword + salt);
        if(!oldPass.equals(byId.getPassword())){
            throw new MyException("原密码输入错误，修改失败");
        }
        String newPassword = SecureUtil.md5(password + salt);
        byId.setPassword(newPassword);
        updateById(byId);
    }

    @Override
    public void resetPassword(Long adminId) {
        Admin byId = getById(adminId);
        if(Objects.isNull(byId)){
            throw new MyException("用户不存在");
        }
        String salt = byId.getSalt();
        String newPassword = SecureUtil.md5("123456" + salt);
        byId.setPassword(newPassword);
        updateById(byId);
    }

    @Override
    public List<Long> listByStreet(String street) {
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.eq("street",street);
        List<Admin> adminList = list(adminQueryWrapper);
        return adminList.stream().map(Admin::getId).collect(Collectors.toList());

    }

}
